package com.nhncorp.ntaf;

public class NtafParallelIterate extends NtafDoTraverse {

	public NtafParallelIterate(NtafExecuteData data) {
		super();
		this.flowFixture = data.getFlowFixture();
		this.data = data;
		this.bContinue = false;
		this.data.initializeParallelIterateAttributes();
	}

	protected Object execute() {
		Object result = null;

		if (false == checkPreConditions("Paralleliterate:")) {
			return null;
		}

		for (cnt = 0; cnt < data.getIterateList().size(); cnt++) {
			prepareIterateValue();

			createParallelIterateThreads();

			runThreads();

			if (bContinue) {
				bContinue = false;
				continue;
			}
		}

		//remove the value of attributes from the symbol list
		data.teardownAttributesAndSymbols();

		return result;
	}

	private void createParallelIterateThreads() {
		for (tableIndex = data.getFrom(); tableIndex < data.getTo(); tableIndex++) {

			String keywordname = data.getKeywordText(tableIndex);

			if (NtafInfo.isKeyword(keywordname)) {
				if (isRunnableKeyword(keywordname)) {
					NtafThread thread = new NtafThread(data.getTables(), tableIndex, data.getTableListener(),
						data.getChildState(), flowFixture);
					addThread(thread);

					//Move index to next table group
					tableIndex = flowFixture.getEndTableIndex(data.getTables(), tableIndex);
				} else if (runSpecialKeywordWithBreakCondition(keywordname)) {
					break;
				}
			} else {
				NtafThread thread = executeParallelFixture(data.getTables().table(tableIndex), data.getTestResults());
				addThread(thread);
			}
		}
	}
}
